// pages/shopping/payment/payment.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    order_id:'',//订单id
    orderInfo:{},//订单详情
    payment_icon: '../../../images/payment_icon.png',
    choose_r: '../../../images/choose_r.png',
    pay_way:1,//支付方式
    // pay_list:[
    //   {
    //     id:0,
    //     pay_way:1,
    //     name:'线上支付',
    //     tip:'使用微信支付',
    //     pic:'../../../images/weixin.png',
    //     checked: 1, //支付方式
    //   }
    // ],
    checked: 1, //支付方式
    disabled:false,
    transfer_pay: false,//是否支持对公转账
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //console.log(options)
    var that = this;
    that.setData({
      order_id: options.order_id
    })
    that.getOrderInfo();
    that.getTransferConfig();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    //如果 isBack 为 true，就返回上一页
    if (that.data.isBack) {
      wx.navigateBack()
    }
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  //选择支付方法
  radioChange: function (e) {
    //console.log(e.detail.value);
    this.setData({
      pay_way: e.detail.value
    })
  },


  //线上支付
  payment:function(){
    // wx.navigateTo({
    //   url: '../paySuccess/paySuccess?type="ok"',
    // })
    
    var that = this;
    that.setData({
      disabled:true
    })
    var order_id = that.data.order_id;
    var pay_way = that.data.pay_way;
    // wx.navigateTo({
    //   url: '../paySuccess/paySuccess?order_id=' + order_id,
    // })
    app.ajax({
      url: 'User/Order/pay',
      method: "POST",
      data: { 
        order_id: order_id,
        pay_way: pay_way
      },
      success: function (res) {
        //console.log(res)
        if (res.data.code == 1000 && (pay_way == 1 || pay_way == 2)) {
          var jsapi = res.data.data.jsapi;
          wx.requestPayment({
            timeStamp: jsapi.timeStamp,
            nonceStr: jsapi.nonceStr,
            package: jsapi.package,
            signType: jsapi.signType,
            paySign: jsapi.paySign,
            'success': function (res) {
              that.setData({
                disabled: false
              })
              app.showToast("支付成功");
              wx.navigateTo({
                url: '../paySuccess/paySuccess?order_id=' + order_id,
              })
            },
            'fail': function (res) {
              that.setData({
                disabled: false
              })
              app.showToast("支付失败");
             // console.log(res);
            }
          })
        } else if (res.data.code == 1000 && pay_way==4){
           that.setData({
              disabled: false
            })           
            app.showToast("支付成功 2s后跳转列表");
            var timer = setTimeout(function () {
              wx.navigateBack({
                delta: 2
              })
            }, 2000)
        }else{
          app.showToast(res.data.message);
        }
        
      }
    })
  },
 
  //查询订单详情
  getOrderInfo:function(){
    var that = this;
    var order_id = that.data.order_id;
    app.ajax({
      url: 'User/Order/getOrderDetail',
      method: "POST",
      data: { order_id: order_id },
      success: function (res) {
        //console.log(res)
        if (res.data.code == 1000) {
          that.setData({
            orderInfo: res.data.data
          });
        } else {
          app.showToast(res.data.message);
        }
      }
    })
  },

  //获取是否对公转账
  getTransferConfig() {
    var that = this;
    var order_id = that.data.order_id;
    app.ajax({
      url: 'User/Work/getUserClass',
      data: {
        order_id: order_id,
        key: 'enginer_transfer_pay'
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.code == 1000 && (res.data.data.user_class == 2 || res.data.data.user_class == 3) && res.data.data.key == 1) {//如果是企业用户并且后台开启对公转账
          that.setData({
            transfer_pay: true
          })
        }
      }
    })
  }



})